<template>
  <div class="login-container">
    <el-card class="login-box">
      <h3>图书管理系统登录</h3>
      <el-form :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item prop="account">
          <el-input
            v-model="loginForm.account"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleLogin() {
      let loginForm = qs.stringify(this.loginForm);
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$axios
            .post("/book/doLogin", loginForm)
            .then((res) => {
              if (res.data) {
                let userInfo = res.data;
                this.$message.success(`${userInfo.account}登录成功！`);
                this.$store.commit("login", userInfo);
                this.$router.push({ path: "/bookManager" });
              } else {
                this.$message.error("登录名或密码错误");
              }
            })
            .catch(() => {
              this.$message.error("请求失败");
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(#667eea 20%, pink 100%,skyblue 40%);
}

.login-box {
  width: 420px;
  padding: 50px 40px;
  border-radius: 15px;
  background: rgba(157, 200, 240, 0.95);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-box:hover {
  transform: translateY(-8px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

h3 {
  text-align: center;
  color: #2d3748;
  font-size: 28px;
  margin-bottom: 35px;
  font-weight: 700;
  letter-spacing: 1.2px;
}

.el-button {
  width: 100%;
  margin-top: 20px;
  padding: 12px 0;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  color: #000000;
  letter-spacing: 2px;
  background-color: #ffffff;
  border: none;
  transition: all 0.3s ease;
}

.el-button:hover {
  background-color: #7db2eb;
  transform: scale(0.98);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

::v-deep .el-input__inner {
  height: 50px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 0 15px;
  font-size: 14px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

::v-deep .el-input__inner:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
  outline: none;
}

::v-deep .el-form-item {
  margin-bottom: 25px;
}

::v-deep .el-form-item__error {
  font-size: 13px;
  margin-top: 5px;
}
</style>
